/**
 * Copyright (c) 2013 onionframework.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.onionframework.servlet;

import java.util.Arrays;

import javax.servlet.Filter;

import org.apache.commons.lang.StringUtils;

/**
 * 过滤器包装类
 * 
 * @author allan
 *
 */
public class FilterDelegate{

	private int index = 1000;
	private Filter target;
	private String[] urlPatterns;
	private String name;
	
	public FilterDelegate(){
	}
	
	
	public FilterDelegate(Filter target, String[] urlPatterns, int index, String name){
		this.target = target;
		this.urlPatterns = urlPatterns;
		this.index = index;
		this.name = name;
	}
	
	/**
	 * @return 执行顺序
	 */
	public int getIndex() {
		return index;
	}
	
	/**
	 * @param index the index to set
	 */
	public void setIndex(int index) {
		this.index = index;
	}
	
	/**
	 * @return 实际的过滤器对象
	 */
	public Filter getTarget() {
		return target;
	}
	
	/**
	 * @param target the target to set
	 */
	public void setTarget(Filter target) {
		this.target = target;
	}
	
	public String[] getUrlPatterns() {
		return this.urlPatterns;
	}

	public void setUrlPatterns(String[] urlPatterns) {
		this.urlPatterns = urlPatterns;
	}

	public String getName() {
		if(StringUtils.isNotBlank(this.name))
			return this.name;
		else if(this.target != null)
			return this.target.getClass().getSimpleName();
		else
			return null;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + index;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((target == null) ? 0 : target.hashCode());
		result = prime * result + Arrays.hashCode(urlPatterns);
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FilterDelegate other = (FilterDelegate) obj;
		if (index != other.index)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (target == null) {
			if (other.target != null)
				return false;
		} else if (!target.equals(other.target))
			return false;
		if (!Arrays.equals(urlPatterns, other.urlPatterns))
			return false;
		return true;
	}


}
